/*
	@author : "likaituan",
	@email : "61114579@qq.com",
	@version : "4.0",
	@date : "2010-09-05",
	@lastime : "2011-8-18"
*/


~function(F){
	//日历类
	F.calendar = F.Class({
		init : function (box){
			F.css(F.data("img/calendar/calendar.css"));
			this.box = F(box).cls("F_calendar");
			this.itemclick = new Function();
			this.w = 36;
			this.initUI();
			F.get(F.data("data.php?act=getdate")).callback(F.proxy(this,this.chkday));
			return this;
		},
		//设置显示的时间
		chkday : function(sd){
			sd = sd.split("-");
			var d = new Date();		
			var y = [d.getFullYear(), +sd[0]];
			var m = [d.getMonth()+1, +sd[1]];
			var i = +(!(y[0]==y[1]&&m[0]==m[1])&&window.confirm("您的本机时间与北京时间不同,是否设定为北京时间？"));
			this.y = y[i];
			this.m = m[i];
			this.up(0, 0);
		},
		//设置显示的时间
		setday : function(d){	
			this.y = d.getFullYear();
			this.m = d.getMonth() + 1;
			return this.up(0,0);
		},
		//初始化界面
		initUI : function(){
			this.ym(-1,0,"<<").ym(0,-1,"<").ti().ym(0,1,">").ym(1,0,">>");
			this.wk("日").wk("一").wk("二").wk("三").wk("四").wk("五").wk("六");
			this.space = this.box.append("div").cls("space");
			this.days = [];
			var fun = function(d){
				var t = [this.y,this.m,d].join('-');
				if(this.hms){
					var selects = this.hms.tags("select");
					var a = [];
					for(var i=0; i<selects.length; i++){
						a[i] = selects[i].value;	
					}
					t += " " + a.join(":");
				}
				this.itemclick(t);
			};
			for(var i=0; i<31; i++){
				this.days[i] = this.box.append("a").attr("href","javascript:void(0);").html(i+1).click(F.proxy(this,fun,i+1));
			}
			return this;
		},
		//标题HTML
		ti : function(){
			var fo = this.box.append("b").cls("dateCap").html('<span></span>年<span></span>月');
			this.year = fo.child(0);
			this.moon = fo.child(1);
			return this;
		},
		//年月跳转HTML
		ym : function(y, m, bn){
			this.box.append("b").cls("btn").click(F.proxy(this,this.up,y,m)).html(bn);
			return this;
		},
		//星期HTML
		wk : function(s){
			this.box.append("b").html(s);
			return  this;
		},
		//绑定到控件中
		input : function (id){
			var obj = F(id).node;
			obj.onfocus = F.proxy(this,this.show,obj);
			return this;
		},
		//增加事件
		on : function(handler, fun){
			this[handler] = fun;
			return this;
		},
		//更新HTML
		up : function(y,m){
			y += this.y;
			m += this.m;
			m==13 && (m=1,y++) || m==0 && (m=12,y--);
			this.year.html(y);
			this.moon.html(m);
			this.space.css("w", new Date(y,m-1,1).getDay()*this.w);
			var totaldays = new Date(y, m, 0).getDate();
			for(var i=28; i<31; i++){
				this.days[i].node.style.display = i<totaldays ? "block" : "none";
			}
			this.y = y;
			this.m = m;
			return this;
		},	
		//动态添加层
		getbox : function (){
			if(F.isie6){
				this.ifr = F().append("iframe").css("cssText","position:absolute; display:none; width:0px; height:0px");
			}
			return F().append("div").cls("cal2").attr("title","点击面板空白处关闭").click(F.proxy(function(e){
				(document.all||e.target==this.obj) && this.hide();
			},this)).hide();
		},
		//选中
		select : function (d){
			this.input.value = d;
			this.hide();
		},
		//显示
		show : function (obj){
			this.input = obj;
			var o = F(obj).abspos(1);
			this.box.pos(o.x, o.y+obj.offsetHeight).show();
			this.ifr && this.ifr.show().sync(this.box,1);
		},
		//隐藏
		hide : function (){
			this.ifr && this.ifr.hide();
			this.box.hide();
		},
		//格式化时分秒
		format : function (str){
			var o = {};
			str.replace(/./g,function(s){o[s]=1});
			var hops = [], mops = [], sops = [];
			for(var i=0; i<60; i++){
				o.h && i<24 && hops.push('<option>'+i+'</option>');
				o.m && mops.push('<option>'+i.zero()+'</option>');
				o.s && sops.push('<option>'+i.zero()+'</option>');		  
			}
			hops = o.h ? '<span><select>'+hops.join("")+'</select>时</span>' : '';
			mops = o.m ? '<span><select>'+mops.join("")+'</select>分</span>' : '';	
			sops = o.s ? '<span><select>'+sops.join("")+'</select>秒</span>' : '';
			this.hms = this.box.append("span").cls("time").html(hops+mops+sops);
		}
	});
}($1stjs);